﻿using System;
using System.Web.UI;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// EditTache 的摘要说明。
    /// </summary>
    public partial class EditTachePage : Page
    {
        private bool bIsExistStep;
        private int _flowId;
        private int _stepId;

        protected override void OnInit(EventArgs e)
        {
            radPassNumYes.CheckedChanged += radPassNumYes_CheckedChanged;
            radPassNumNo.CheckedChanged += radPassNumNo_CheckedChanged;
            chkUrgencyAlert.CheckedChanged += chkUrgencyAlert_CheckedChanged;
            cmdPrevious.Click += cmdPrevious_Click;
            cmdNext.Click += cmdNext_Click;
            cmdDelete.Click += cmdDelete_Click;
            cmdFinish.Click += cmdFinish_Click;
            cmdCancel.Click += cmdCancel_Click;
            cmdReturn.Click += cmdReturn_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            _flowId = QSUtil.GetPositiveInt32("FlowID");
            _stepId = QSUtil.GetPositiveInt32("StepID");

            if (!Page.IsPostBack)
            {
                Bangding();
            }

            base.OnLoad(e);
        }

        private void Bangding()
        {
            bIsExistStep = _stepId > FlowStep.GetMaxStep(_flowId) ? false : true;
            labStep.Text = "步骤：第 " + _stepId + " 步";
            labFlowName.Text = Flow.GetFlowTitle(_flowId);

            if (bIsExistStep)
            {
                FlowStep step = FlowStep.GetStep(_flowId, _stepId);
                if ( step != null )
                {
                    txtTacheName.Text = step.Name;
                    txtRemark.Text = step.Description;

                    if (step.RightToFinish)
                    {
                        chkFinishYes.Checked = true;
                        chkFinishNO.Checked = false;
                    }
                    else
                    {
                        chkFinishYes.Checked = false;
                        chkFinishNO.Checked = true;
                    }
                    txtPassNum.Text = step.PassNum.ToString();
                    if (step.PassNum == 0)
                    {
                        radPassNumYes.Checked = false;
                        radPassNumNo.Checked = true;
                        PassNum.Style["display"] = "none";
                    }
                    else
                    {
                        radPassNumNo.Checked = false;
                        radPassNumYes.Checked = true;
                        PassNum.Style["display"] = "";
                        if (Int32.Parse(txtPassNum.Text) == -1)
                        {
                            chkAllPass.Checked = true;
                        }
                        else
                        {
                            chkAllPass.Checked = false;
                        }
                    }
                    for (int i = 0; i < cboFlowRule.Items.Count; i++)
                    {
                        if (cboFlowRule.Items[i].Value == ((int)step.FlowRule).ToString())
                        {
                            cboFlowRule.SelectedIndex = i;
                        }
                    }
                    chkLocalAlert.Checked = step.LocalAlert;
                    if (step.CycTimes > 0)
                    {
                        chkUrgencyAlert.Checked = true;
                        spanAlert.Style["display"] = "";
                        txtBaseHour.Text = step.BaseHour.ToString();
                        txtCycTimes.Text = step.CycTimes.ToString();
                        txtPeriod.Text = step.Period.ToString();
                    }
                    else
                    {
                        chkUrgencyAlert.Checked = false;
                        spanAlert.Style["display"] = "none";
                        txtBaseHour.Text = "0";
                        txtCycTimes.Text = "0";
                        txtPeriod.Text = "0";
                    }
                }
                cmdDelete.Enabled = true;
            }
            else
            {
                spanAlert.Style["display"] = "none";
                txtBaseHour.Text = "0";
                txtCycTimes.Text = "0";
                txtPeriod.Text = "0";
                cmdDelete.Enabled = false;
            }

            cmdCancel.Attributes.Add("onclick", "return confirm('是否删除此流程？');");
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            int stepId;
            if (SaveCurrentStep(out stepId))
            {
                Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=" + (++stepId));
            }
            else
            {
                Response.Redirect("~/SubModule/Error.aspx");
            }
        }

        private void cmdFinish_Click(object sender, EventArgs e)
        {
            int stepId;
            if (SaveCurrentStep(out stepId))
            {
                Response.Redirect("ManageFlow.aspx?FlowID=" + _flowId);
            }
            else
            {
                Response.Redirect("~/SubModule/Error.aspx");
            }
        }

        private bool SaveCurrentStep(out int stepId)
        {
            int iPassNum;
            if (radPassNumNo.Checked)
            {
                iPassNum = 0;
            }
            else
            {
                iPassNum = Int32.Parse(txtPassNum.Text);
            }

            int iBaseHour;
            int iCycTimes;
            int iPeriod;

            if (chkUrgencyAlert.Checked)
            {
                iBaseHour = Int32.Parse(txtBaseHour.Text);
                iCycTimes = Int32.Parse(txtCycTimes.Text);
                iPeriod = Int32.Parse(txtPeriod.Text);
            }
            else
            {
                iBaseHour = 0;
                iCycTimes = 0;
                iPeriod = 0;
            }

            //判断此流程的步骤是否存在			
            if (_stepId > FlowStep.GetMaxStep(_flowId))
            {
                // Create a new step for the flow.
                stepId = FlowStep.AddStep(_flowId, txtTacheName.Text, txtRemark.Text, chkFinishYes.Checked, cboFlowRule.SelectedRule, iPassNum, chkLocalAlert.Checked, iBaseHour, iCycTimes, iPeriod);
                return (stepId > 0);
            }

            // It seems the specified step already exists.
            stepId = _stepId;
            return (FlowStep.UpdateStep(_flowId, _stepId, txtTacheName.Text, txtRemark.Text, chkFinishYes.Checked ? 1 : 0, Int32.Parse(cboFlowRule.SelectedItem.Value), iPassNum, chkLocalAlert.Checked ? 1 : 0, iBaseHour, iCycTimes, iPeriod) == 0);
        }

        private void radPassNumYes_CheckedChanged(object sender, EventArgs e)
        {
            PassNum.Style["display"] = "";
        }

        private void radPassNumNo_CheckedChanged(object sender, EventArgs e)
        {
            PassNum.Style["display"] = "none";
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            //删除流程
            if (Flow.DeleteFlow(_flowId) == 0)
            {
                Response.Redirect("Listview.aspx");
            }
            else
            {
                Response.Redirect("../../Error.aspx");
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            //删除此步
            FlowStep.DeleteStep(_flowId, _stepId);

            if (_stepId > 1)
            {
                Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=" + (--_stepId));
            }
            else
            {
                Response.Redirect("EditFlow.aspx?FlowID=" + _flowId);
            }
        }

        private void cmdPrevious_Click(object sender, EventArgs e)
        {
            if (_stepId > 1)
            {
                Response.Redirect("EditTache.aspx?FlowID=" + _flowId + "&StepID=" + (--_stepId));
            }
            else
            {
                Response.Redirect("EditFlow.aspx?FlowID=" + _flowId);
            }
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageFlow.aspx?FlowID=" + _flowId);
        }

        private void chkUrgencyAlert_CheckedChanged(object sender, EventArgs e)
        {
            if (chkUrgencyAlert.Checked)
            {
                spanAlert.Style["display"] = "";
            }
            else
            {
                spanAlert.Style["display"] = "none";
            }
        }
    }
}